﻿using _2._13._2015_VehiclesInClass.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace _2._13._2015_VehiclesInClass
{
    public sealed class VehicleService
    {
        #region Singleton Stuff

        private static VehicleService instance = null;

        private VehicleService()
        {
            _vehicles = new List<Vehicle>();
        }

        public static VehicleService Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new VehicleService();
                }

                return instance;
            }
        }

        #endregion

        private List<Vehicle> _vehicles;
        private int _currId;

        public List<Vehicle> GetVehicles()
        {
            return _vehicles.OrderBy(v => v.Make).ToList();
        }

        public int CreateCar(Car car)
        {
            car.Id = ++_currId;
            _vehicles.Add(car);

            return car.Id;
        }

        public void CreateCar(string make, string model, int year)
        {
            _vehicles.Add(new Car
                {
                    Make = make,
                    Model1 = model,
                    Year = year,
                    Id = ++_currId
                });
        }

        public void UpdateCar(Car car)
        {
            Vehicle dbCar = _vehicles.FirstOrDefault(c => c.Id == car.Id);
            dbCar.Make = car.Make;
            dbCar.Model1 = car.Model1;
            dbCar.Year = car.Year;
        }

        public void UpdateCar(int id, string make, string model, int year)
        {
            Vehicle dbCar = _vehicles.FirstOrDefault(c => c.Id == id);
            dbCar.Make = make;
            dbCar.Model1 = model;
            dbCar.Year = year;
        }

        public Vehicle GetCar(int id)
        {
            return _vehicles.FirstOrDefault(c => c.Id == id);
        }
    }
}